/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package taw2012;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mario rossi
 */
public class Main extends Thread {

    final int REFRESH_INT = 12*60*60*1000;
    private volatile Thread running; 
    private String log = "";
    private GestioneDB manager;
    private LettoreCsv a;
    public Main(){
        manager =new GestioneDB();
        a=new LettoreCsv();
        manager.inizializzaDB();
        a.LeggiCsv();
    }
    
   public void stopThread() {
        running = null;
        log = new Date() + ": Thread fermato.\n" + log;
    }
    
    // Recupera il log del thread.
    public String getLog() {
        return log;
    }
    
    // Svuota il log del thread.
    public void emptyLog() {
        log = "";
    }
    @Override
    public void run() {
          try {
           
            log = new Date() + ": Inizio aggiornamento database.\n" + log;
            
            manager.connetti();
            //manager.stampaCitta();
            manager.aggiornaPrevisioniDB();
            //manager.stampaPrevisioni();
            manager.disconnetti();
            log = new Date() + ": Fine aggiornamento database.\n" + log;
            Thread.sleep(this.REFRESH_INT);
        }
        catch (InterruptedException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            log+=new Date() + ": Failed to start.\n";
            System.err.println(ex);
        }
        
    }

    @Override
    public synchronized void start() {
        running = new Thread(this);
        running.start();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        /* Prova connessioneDB*/
       Main m = new Main();
       m.start();
       m.run();
       System.out.println(m.getLog());
       
       
    }

}
